package com.geostar.constructionland.analyse.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geostar.constructionland.analyse.mapper.DlbmVersionMapper;
import com.geostar.constructionland.common.entity.dlbm.DlbmVersion;
import com.geostar.constructionland.common.entity.response.ExtjsResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;

/**
 * @author chengan
 * @description 地类编码版本号
 */
@Controller
@RequestMapping("/dlbmVersion")
@Api(value = "分析版本接口")
public class DlbmVersionController {

    @Autowired
    DlbmVersionMapper dlbmVersionMapper;


    @PostMapping("/add")
    @ResponseBody
    @ApiOperation(value = "添加版本信息")
    public ExtjsResponse addDlbmVersion(@RequestBody DlbmVersion dlbmVersion) {
        DateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
        try {
            dlbmVersion.setStartTime(format.parse(dlbmVersion.getStartTimeString()));
            dlbmVersion.setEndTime(format.parse(dlbmVersion.getEndTimeString()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        dlbmVersionMapper.insert(dlbmVersion);
        return ExtjsResponse.createBySuccessMessage("添加地类编码版本信息成功！");
    }


    @PostMapping("/del")
    @ResponseBody
    @ApiOperation(value = "删除版本信息")
    public ExtjsResponse deleteDlbmVersion(@NotBlank(message = "版本ID不能为空") @RequestParam(value = "versionId") String versionId) {
//        dlbmVersionMapper.deleteById(versionId);
        if (versionId.contains(",")) {
            // 将多个id处理成集合形式
            List<String> versionIds = Arrays.asList(versionId.split(","));
            dlbmVersionMapper.deleteBatchIds(versionIds);
        } else {
            dlbmVersionMapper.deleteById(versionId);
        }
        return ExtjsResponse.createBySuccessMessage("删除地类编码版本信息成功！");
    }

    @PostMapping("/update")
    @ResponseBody
    @ApiOperation(value = "更新版本信息")
    public ExtjsResponse updateDlbmVersion(@RequestBody DlbmVersion dlbmVersion) {
        DateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
        try {
            dlbmVersion.setStartTime(format.parse(dlbmVersion.getStartTimeString()));
            dlbmVersion.setEndTime(format.parse(dlbmVersion.getEndTimeString()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return ExtjsResponse.createBySuccess(dlbmVersionMapper.updateById(dlbmVersion));
    }


    @PostMapping("/getVersions")
    @ResponseBody
    @ApiOperation(value = "获取版本信息")
    public ExtjsResponse pageInfoDlbmVersion(@NotBlank(message = "页码不能为空") @RequestParam(value = "page") int pageNo,
                                             @NotBlank(message = "每页数量不能为空") @RequestParam(value = "limit") int pageNum,
                                             @RequestParam("versionNumber") String versionNumber) {
        LambdaQueryWrapper<DlbmVersion> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(versionNumber)) {
            queryWrapper.like(DlbmVersion::getVersionNumber, versionNumber);
        }
        Page<DlbmVersion> page = new Page<>(pageNo, pageNum);
        IPage<DlbmVersion> dlbmVersionPage = dlbmVersionMapper.selectPage(page, queryWrapper);
        return ExtjsResponse.createBySuccess(dlbmVersionPage);
    }


}
